﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Http;
using System.Web.Http.Dispatcher;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Net;
using Bzure.Mvc.Controllers;
using Bzure.Common.Core;

namespace Bzure.Mvc
{
    [Export(typeof(IApplicationStart)), ExportMetadata("ProviderName", "BzureMvcStarter")]
    public class SecurityAppStarter : IApplicationStart
    {

        /// <summary>
        /// 契约实现的版本号
        /// </summary>
        public float ContractVersion { get { return 1.0F; } }
        public void PreStart(IApplicationStartContext StartContext)
        {
            if (Actor.Public.AppSections().IsNotNullAndEmpty())
            {
                StartContext.SetMvcDefaultArea(() =>
                {
                    return Actor.Public.AppSections().OrderBy(m => m.Index).First().SectionName;
                });
            }
            ControllerBuilder.Current.SetControllerFactory(new Bzure.Mvc.Controllers.ThemeControllerFactory());
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new Bzure.Mvc.Views.WebFormThemeViewEngine());
            ViewEngines.Engines.Add(new Bzure.Mvc.Views.RazorThemeViewEngine());
            GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), new ThemeHttpControllerSelector(GlobalConfiguration.Configuration));
        }
        public void Start(IApplicationStartContext StartContext)
        {
        }
    }
}
